作者:手浪用户2602928705 | 来源:互联网 | 2023-08-07 19:28
C#|如何获取排序集中的子集原文:https://www.
C# |如何获取排序集中的子集
原文:https://www . geeksforgeeks . org/c-sharp-如何在排序集中获取子集/
SortedSet 类表示按排序顺序排列的对象集合。这个班隶属于T3 系统。集合.通用命名空间。排序集。GetViewBetween(T,T)方法用于返回排序集合中子集的视图。
属性:
- 在 C# 中,SortedSet 类可用于存储、移除或查看元素。
- 它保持升序,不存储重复的元素。
- 如果必须存储唯一元素并保持升序,建议使用 SortedSet 类。
语法:公共虚拟系统。集合.泛型. sorted set<T>getview between(T 下值,T 上值);
参数:
下限数值:视图中的最低期望值。
上限值:视图中的最高期望值。
返回值:仅包含指定范围内的值的子集视图。
异常:
- 参数异常:如果下值大于上值根据比较器。
- argumentoutofrangerexception:视图上的尝试操作超出了下值和上值指定的范围。
例 1:
// C# code to get the subset of a SortedSet
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a SortedSet of strings
SortedSet mySet1 = new SortedSet();
// Inserting elements in SortedSet
mySet1.Add("A");
mySet1.Add("B");
mySet1.Add("C");
mySet1.Add("D");
mySet1.Add("E");
mySet1.Add("F");
mySet1.Add("G");
mySet1.Add("H");
mySet1.Add("I");
// Get the subset between "C" and "G"
SortedSet mySet2 = mySet1.GetViewBetween("C", "G");
// Displaying the elements in the subset
foreach(string str in mySet2)
{
Console.WriteLine(str);
}
}
}
Output:
例 2:
// C# code to get the subset of a SortedSet
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a SortedSet of integers
SortedSet<int> mySet1 = new SortedSet<int>();
// Inserting elements in SortedSet
for (int i = 0; i <10; i++) {
mySet1.Add(i);
}
// Get the subset between "3" and "7"
SortedSet<int> mySet2 = mySet1.GetViewBetween(3, 7);
// Displaying the elements in the subset
foreach(int i in mySet2)
{
Console.WriteLine(i);
}
}
}
Output:
参考:
- https://docs . Microsoft . com/en-us/dotnet/API/system . collections . generic . sorted set-1 . getview better?view=netcore-2.1